package com.example.service.impl;


import com.example.service.FileService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 *
 */
@Service
public class FileServiceImpl implements FileService {



    @Override
    public String upload(MultipartFile uploadFile, String dir_path) {
        File folder = new File(dir_path);
        if (!folder.exists() && !folder.isDirectory()) {
            folder.mkdirs();
        }
        String oldName = uploadFile.getOriginalFilename();
        // String extendName = oldName.substring(oldName.lastIndexOf("."), oldName.length());
        String newName = UUID.randomUUID().toString() + "@" + oldName;
        String filePath = "";
        try {
            uploadFile.transferTo(new File(folder, newName));
            filePath = dir_path + "/" + newName;

        } catch (IOException e) {
            e.printStackTrace();
            return "上传失败! ";
        }

        return filePath;
    }

}
